/*
* comment.cpp - This file is the part of the wp-notifier.(code name "luan luan")
* 
Copyright (C) 2010 Dean Lee <deanlee3@gmail.com>, http://www.deanlee.cn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
* Acknowledgements:
*   <> Many thanks to all of you, who have encouraged me to update my articles
*     and code, and who sent in bug reports and fixes.
* 
*/
#include <QMap>
#include <QVariant>
#include "comment.h"
#include "common.h"
Comment::Comment()
{
	d = new CommentData();
	/*m_comment_id = m_siteid = m_user_id = m_comment_parent = m_post_id = m_read_flag = 0;
	m_status = approve;
	m_unread = false;*/
}

Comment::Comment(const Comment& other) : d(other.d)
{
}


CommentData::CommentStatus Comment::stringToStatus(const QString& status)
{
	CommentData::CommentStatus s = CommentData::approve;
	if (status == "approve") {
		s = CommentData::approve;
	}
	else if (status == "hold") {
		s = CommentData::hold;
	}
	else if (status == "spam") {
		s = CommentData::spam;
	}
	return s;
}

QString Comment::statusToString(CommentData::CommentStatus status)
{
	QString s;
	switch (status){
	case CommentData::approve:
		s = "approve";
		break;
	case CommentData::hold:
		s = "hold";
		break;
	case CommentData::spam:
		s = "spam";
		break;
	}
	return s;
}
bool Comment::fromMap(const QVariant& param)
{
	QMap<QString, QVariant> p = param.toMap();
	d->m_comment_id = p.value("comment_id").toInt();
	d->m_user_id = p.value("user_id").toInt();

	d->m_comment_parent = p.value("parent").toInt();
	d->m_status = Comment::stringToStatus(p.value("status").toString());
	d->m_link = p.value("link").toString();
	d->m_post_id = p.value("post_id").toInt();
	d->m_author_url = p.value("author_url").toString();
	d->m_author_email = p.value("author_email").toString();
	d->m_author_ip = p.value("author_ip").toString();
	d->m_dateCreated = p.value("date_created_gmt").toDateTime();
	d->m_content = QString::fromUtf8(p.value("content").toString().toAscii());
	d->m_author = QString::fromUtf8(p.value("author").toString().toAscii());
	d->m_post_title = QString::fromUtf8(p.value("post_title").toString().toAscii());
	d->m_dateDiff = dateDiffString(d->m_dateCreated);
	return true;
}

//QList<Comment*> Comment::responseToCommentList(const QVariant& response)
//{
//	QList<Comment*> result;
//	if (response.type()!= QVariant::List)
//		return result;
//	QList<QVariant> comments = response.toList();
//	foreach(QVariant v, comments) {
//		QMap<QString, QVariant> p = v.toMap();
//		Comment* c = mapToComment(p);
//		result << c;
//	}
//	return result;
//}
